package com.yuyou.fn.tradingservice.controller;

import com.yuyou.fn.platform.entity.SysUser;
import com.yuyou.fn.tradingservice.entity.ServiceCartItem;
import com.yuyou.fn.tradingservice.entity.ServiceOrder;
import com.yuyou.fn.tradingservice.entity.ServiceProduct;
import com.yuyou.fn.tradingservice.entity.ShippingAddress;
import com.yuyou.fn.tradingservice.service.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by Terry on 2017/4/25.
 */
@Controller
@RequestMapping(value="/trading/product")
public class TradingProductController extends TradingServiceBaseController  {
    @Resource
    private IServiceProductService productService;

    @Resource
    private IShippingAddressService addressService;

    @Resource
    private IServiceCartItemService cartItemService;

    @Resource
    private IServiceCartService cartService;

    @Resource
    private IServiceOrderService orderService;

    @ApiOperation("立即购买提交订单")
    @RequestMapping(value = "/submitOrder", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String submitOrder(@RequestParam(value = "addressId",defaultValue = "0") String addressId,
                              HttpServletRequest request, @RequestParam(value="productId") String productId, int quantity) {
        //获取当前登录用户
        SysUser user = getUser(request);
//        SysUser user = new SysUser();
//        user.setUserId(1L);
//        user.setUsername("yuyou");
        ServiceOrder order = orderService.generateSingleOrder(Long.parseLong(addressId), user, Long.parseLong(productId), quantity);
        return order.getOrderId().toString();
    }

    @ApiOperation("显示提交订单")
    @RequestMapping(value = "/showOrder", method = {RequestMethod.GET, RequestMethod.POST})
    public String showOrder(@RequestParam(value = "id") Long orderId,ModelMap model) {
        ServiceOrder order = orderService.findById(orderId);
        model.addAttribute("order", order);
        return "product/pay-product";
    }

    @ApiOperation("前端商品点击购买加入订单")
    @RequestMapping(value = "buyNow", method = {RequestMethod.GET, RequestMethod.POST})
    public String buyNow(ModelMap model, @RequestParam(value = "productId") Long productId, @RequestParam(value = "quantity") int quantity, HttpServletRequest request) {
        SysUser user = getUser(request);
        if(user == null) {
            return "login";
        }
        ServiceProduct product = productService.findById(productId);
        List<ShippingAddress> addressList = addressService.findAddressListByUserId(user.getUserId());
        model.addAttribute("product", product);
        model.addAttribute("quantity", quantity);
        model.addAttribute("addressList", addressList);
        return "product/product-order";
    }

    @ApiOperation("购物车中添加商品数量")
    @RequestMapping(value="updateCartItem", method = {RequestMethod.POST})
    @ResponseBody
    public String updateCartItem(@RequestParam(value="cartItemId") Long cartItemId, @RequestParam(value="quantity") int quantity){
        cartItemService.updateCartItem(cartItemId, quantity);
        return "ok";
    }

    @ApiOperation("查看商品购物车")
    @RequestMapping(value="/showCartItems", method = {RequestMethod.POST, RequestMethod.GET})
    public String showCartItems(ModelMap model, @RequestParam(value="type") int type){
        List<ServiceCartItem> cartItemList = cartItemService.getAllCartItems(type);
        model.addAttribute("cartItemList", cartItemList);
        return "product/product-cart";
    }

    @ApiOperation("前端商品加入购物车")
    @RequestMapping(value = "/addProductToCart", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String addProductToCart(@RequestParam("productId") Long productId, @RequestParam("quantity") int quantity,
                                   @RequestParam(value="type") Integer type, HttpServletRequest request) {
        SysUser user = getUser(request);
        if(user == null) {
            return "login";
        }
        ServiceProduct product = productService.findById(productId);
        cartService.addToCart(product, user.getUserId(), quantity);
        return "ok";
    }
}
